package com.wakyde.servlet;

import com.wakyde.pojo.Person;

import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.IOException;

/**
 * @Description: TODO
 * @author: wakyde
 * @date: 2022年05月10日 15:45
 */
public class SessionDemo01 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // 防止乱码
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;character=utf-8");
        // 获取session
        HttpSession session = req.getSession();

        session.setAttribute("person", new Person("1", "灵感个"));

        String sessionId = session.getId();

        if (session.isNew()){
            resp.getWriter().write("session创建成功, ID:" + sessionId);
        }else{
            resp.getWriter().write("session已经存在了, ID:" + sessionId);
        }

        // 创建Session时,都做了什么?
        // Cookie cookie = new Cookie("JSESSIONID", "05CA9A63314C1CBFA4FFF6E4AB9FF167");
        // resp.addCookie(cookie);
    }
}
